package de.herberlin.colormatcher;

import java.awt.Color;

/**
 * Class representing an Red Green Blue color value.
 * @author hans joachim herbertz
 * created 09.07.2003
 */
public class RGB {

	/**
	 * Constructor
	 */
	public RGB() {
		super();
	}
	
	/**
	 * Constructor capable to parse an rgb-string
	 * as "#0AFF03"
	 * @param rgbString
	 */
	public RGB(String rgbString){
		if (rgbString.startsWith("#")){
			rgbString=rgbString.substring(1);
		}
		if (rgbString.length()!=6){
			throw new NumberFormatException(Colormatcher.getBundle().getString("Bad_length_of_RGB-String")+rgbString);
		}
		r=Integer.parseInt(rgbString.substring(0,2),16);
		g=Integer.parseInt(rgbString.substring(2,4),16);
		b=Integer.parseInt(rgbString.substring(4,6),16);
	}
	/**
	 * Creates an RGB from an awt.Color
	 * @param color
	 */
	public RGB(Color color){
		r=color.getRed();
		g=color.getGreen();
		b=color.getBlue();
	}
	public  int r = 0;
	public int g = 0;
	public int b = 0;

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return (getHex(r) + getHex(g) + getHex(b)).toUpperCase();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return (r * 255 * 255) + (g * 255) + b;
	}
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object o) {
		return this.hashCode() == o.hashCode();
	}
	
	/**
	 * Returns the hexadecimal representation of
	 * a given integer
	 * @param z
	 * @return
	 */
	private String getHex(int z) {
		String ret = Integer.toHexString(z);
		if (z < 0x10) {
			return "0" + ret;
		} else {
			return ret;
		}
	}
	/**
	 * Returns the matching java.awt.Color
	 * @return
	 */
	public Color getColor() {
		return new Color(r,g,b);
	}

}
